const r = require("../../../api.js");

var e = require("../../../api.js"), t = getApp(), a = require("../../../utils/function.js"), s = wx.getStorageSync("MAP_KEY") || "PNIBZ-QF5R2-XSGUL-CGVXI-5J4GZ-VNFNX";

Page({
    data: {
        istail:2,
        startAddress: null,
        endAddress: null,
        name: null,
        phone: null,
        isReplacePay: !1,
        order: wx.getStorageSync("order"),
    },
    onLoad: function(e) {
        if(e.istail) {
            this.setData({
           istail:e.istail 
        })
        }
        
        e.uid && t.shareCouponsMessage(e.uid), this.getUserAddress();
    },
    addressName: function(e) {
        var t = this;
        wx.request({
            url: "https://apis.map.qq.com/ws/geocoder/v1/?l&get_poi=1",
            data: {
                key: s,
                location: e
            },
            method: "GET",
            success: function(e) {
                var a = t.data.startAddress, s = e.data.result;
                s.pois[0] && (a = {
                    title: s.pois[0].title,
                    address: s.pois[0].address,
                    location: {
                        lat: s.pois[0].location.lat,
                        lng: s.pois[0].location.lng
                    }
                }), t.setData({
                    startAddress: a
                });
            },
            fail: function() {},
            complete: function() {}
        });
    },
    getUserAddress: function() {
        var e = this;
        wx.getLocation({
            type: "gcj02",
            success: function(t) {
                e.addressName(t.latitude + "," + t.longitude);
            }
        });
    },
    startAddressBtn: function() {
        var e = this;
        wx.chooseLocation({
            success: function(t) {
                e.setData({
                    startAddress: {
                        title: t.name,
                        address: t.address,
                        location: {
                            lat: t.latitude,
                            lng: t.longitude
                        }
                    }
                });
            }
        });
    },
    endAddress: function() {
        var e = this;
        wx.chooseLocation({
            success: function(t) {
                e.setData({
                    endAddress: {
                        title: t.name,
                        address: t.address,
                        location: {
                            lat: t.latitude,
                            lng: t.longitude
                        }
                    }
                });
            }
        });
    },
    getNameHandler: function(e) {
        this.setData({
            name: e.detail.value
        });
    },
    getPhoneHandler: function(e) {
        this.setData({
            phone: e.detail.value
        });
    },
    isReplacePayHandler: function(e) {
        this.setData({
            isReplacePay: e.detail.value
        });
    },
    testNameHandler: function(e, t) {
        // console.log('8578587857');
        if (null !== e && "" !== e) return !0;
         wx.showToast({
            title: "请输入乘车人姓名",
            icon: "none",
            duration: 2e3
        });
    },
    testPhoneHandler: function(e) {
        var t = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1}))+\d{8})$/;
        if (null === e) wx.showToast({
            title: "请输入电话号码",
            icon: "none",
            duration: 2e3
        }); else {
            if (t.test(e)) return !0;
            wx.showToast({
                title: "手机号码格式不对",
                icon: "none",
                duration: 2e3
            });
        }
    },
    create_order: function() {
        // this.submitOrderHandler()
        var s = {
            name: this.data.name,
            phone: this.data.phone,
            replace_pay: this.data.isReplacePay,
            type: 1
        }, n = s.phone, o = s.name, r = s.type, i = s.replace_pay;
        wx.showLoading({});
        var d = this.data.endAddress, l = wx.getStorageSync("OPEN_LIMIT_DES");
        if (!d) {
            if (l) return void a.showToast("请输入目的地", "none");
            this.data.endAddress = this.data.startAddress;
        }
        null != n && 11 == n.length ? t.request({
            url: e.order.place_order,
            method: "post",
            data: {
                startAddress: JSON.stringify(this.data.startAddress),
                endAddress: JSON.stringify(this.data.endAddress),
                driver_count: 1,
                passenger_phone: n,
                passenger_name: o || "",
                paid: i ? 1 : 0,
                type: r,
                about_time: this.data.about_time,
                hours: this.data.hours,
                minute: this.data.minute,
                dat: this.data.date,
                travel_type: this.data.istail
            },
            success: function(s) {
                wx.hideLoading(), 1 == s.code ? (wx.getStorageSync("relative_people") && wx.removeStorageSync("relative_people"), 
                wx.canIUse("requestSubscribeMessage") && t.request({
                    url: e.message.getTmplIds,
                    method: "GET",
                    data: {
                        type: 1
                    },
                    success: function(e) {
                        wx.requestSubscribeMessage({
                            tmplIds: e.data,
                            complete: function(e) {}
                        });
                    }
                }), s.data.orderid ? (wx.setStorageSync("orderid", s.data.orderid), t.driver(s.data.orderid), wx.redirectTo({
                    url: "/pages/map/orderAndDriverGoing/orderAndDriverGoing?order_no=" + s.data.orderid
                })) : a.showToast(s.msg, "none")) : a.showToast(s.msg, "none");
            }
        }) : a.showToast("手机号格式错误", "none");
    },
    submitOrderHandler: function() {
        console.log('被执行了');
        var e = this.testPhoneHandler(this.data.phone), t = this.testNameHandler(this.data.name);
        // if (null !== t && "" !== t) return !0;
        //  wx.showToast({
        //     title: "请输入乘车人姓名",
        //     icon: "none",
        //     duration: 2e3
        // });
        // console.log(t,e);
        // if (!t) wx.showToast({
        //   title: '请不要忘记输入名字',
        // }),
        e && t && (wx.setStorageSync("relative_people", {
            name: this.data.name,
            phone: this.data.phone,
            replace_pay: this.data.isReplacePay,
            type: 1
        }), wx.navigateBack({
            delta: 1
        }));
    },
    deleteHistoryHandler: function() {
        var e = this;
        wx.showModal({
            title: "历史记录",
            content: "确认删除历史记录",
            success: function(t) {
                t.confirm && e.setData({
                    order: []
                }), wx.showToast({
                    title: "删除成功"
                });
            },
            fail: function() {
                wx.showToast({
                    title: "删除失败"
                });
            }
        });
    },
    onReady: function() {},
    onShow: function() {
        this.setData({
            order: wx.getStorageSync("order")
        });
    },
    onHide: function() {},
    onUnload: function() {},
    onPullDownRefresh: function() {},
    onReachBottom: function() {},
    onShareAppMessage: function() {
        return {
            title: t.shareAppMessage().title,
            imageUrl: t.shareAppMessage().imageUrl,
            desc: t.shareAppMessage().desc,
            path: t.shareAppMessage().path
        };
    }
});